package isg3.data;

import isg3.domain.Concurso;
import isg3.domain.Prestamo;
import isg3.utils.UIDGenerator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Date;
import java.sql.SQLException;
import java.util.*;

public class JDBCPrestamoDAO implements IPrestamoDAO {
	
	private Connection conn;
	
	public JDBCPrestamoDAO(){
		conn = ConnectionManager.getInstance().checkOut();
		System.out.println("SE HA CREADO LA CONEXION");
	}

	public void insertPrestamo(String user,String idlib){
		String oido = UIDGenerator.getInstance().getKey();
		String estado = new String("solicitado");
        PreparedStatement stmt = null;
        String sql = "INSERT INTO prestamos ( OIDPrestamo, usuarioOID, libroOID, estado, fechaPres, fechaDev) VALUES (?, ?, ?, ?, ?, ?) ";
        
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, oido);
            stmt.setString(2, user);
            stmt.setString(3, idlib);
            stmt.setString(4, estado);
            stmt.setString(5, "");
            stmt.setString(6, "");

            stmt.executeUpdate();


        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}

	public List selectPrestamosUsuario(String idu) {
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM prestamos WHERE (usuarioOID = ?)"; 
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, idu);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Prestamo temp = new Prestamo();
                temp.setLibro(result.getString("libroOID"));
                temp.setEstado(result.getString("estado"));
                temp.setIdPrestamo(result.getString("OIDPrestamo"));
                temp.setFechaEntrega(result.getDate("fechaDev"));
                temp.setFechaPrestamo(result.getDate("fechaPres"));
       
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}

	public List selectAllPrestamos() {
		
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Prestamos"; 
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Prestamo temp = new Prestamo();
            	temp.setLibro(result.getString("libroOID"));
                temp.setEstado(result.getString("estado"));
                temp.setIdPrestamo(result.getString("OIDPrestamo"));
                temp.setFechaEntrega(result.getDate("fechaDev"));
                temp.setFechaPrestamo(result.getDate("fechaPres"));
        
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}
	
	public void updatePrestamo(String usuario, String libro, String estado, java.util.Date fechaPres, java.util.Date fechaDev){
		PreparedStatement stmt = null;
        String sql = "UPDATE prestamos SET estado = ?, fechaPres = ?, fechaDev = ? WHERE (libroOID = ? AND usuarioOID = ?)";
        try {
        	
            stmt = conn.prepareStatement(sql);
            java.util.Date myDate= fechaPres ;
            java.util.Date myDate2 = fechaDev;
            stmt.setString(1, estado);
            stmt.setDate(2, new java.sql.Date( myDate.getTime()));
            stmt.setDate(3, new java.sql.Date( myDate2.getTime()));
            stmt.setString(4, libro);
            stmt.setString(5, usuario);
            
            stmt.executeUpdate();

            //insert details
           // ddao.insert(conn, o.getDetails(), oido);

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
	}
	
	public void deletePrestamo(String usuario, String libro){
		String sql = "DELETE FROM prestamos WHERE (usuarioOID = ? AND libroOID = ?)";
		PreparedStatement stmt = null;
		
		try{
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, usuario);
			stmt.setString(2, libro);
			stmt.executeUpdate();
		}catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
	}

}
